var markerArray = new Array();
var newMarkerId;
var currentMarker = null;
var googleMap;
var mapBounds;
var infoWindow;

function showInfo () {
    infoWindow.setContent(this.html);
    infoWindow.open(googleMap, this);
}

function selectMarker(marker) {
    if (marker != currentMarker) {
        if (currentMarker != null) {
            currentMarker.setIcon("");
        }
        currentMarker = marker;
        currentMarker.setIcon("../Content/Images/marker_sprite_blue.png");
    }
}

function addMarker(areaName, areaInfo, lat, lng, dragEnabled) {
    var thisMarkerId = '[' + newMarkerId++ + ']';
    var markerLocation = new google.maps.LatLng(lat, lng);
    mapBounds.extend(markerLocation);
    selectMarker(new google.maps.Marker({ title: areaName, position: markerLocation, map: googleMap, draggable: dragEnabled, html: areaInfo }));
    currentMarker.markerId = thisMarkerId;
    markerArray[thisMarkerId] = currentMarker;
    // Eventos del pushpin
    google.maps.event.addListener(currentMarker, "rightclick", showInfo);
    google.maps.event.addListener(currentMarker, "dblclick", showInfo);
    if (dragEnabled) {
        google.maps.event.addListener(currentMarker, "click", function () {
            selectMarker(this);
            infoWindow.close();
        });
        google.maps.event.addListener(currentMarker, "dragstart", function () {
            infoWindow.close();
        });
        google.maps.event.addListener(currentMarker, "dragend", function () {
            document.getElementsByName(this.markerId + '.Latitude')[0].value = this.getPosition().lat();
            document.getElementsByName(this.markerId + '.Longitude')[0].value = this.getPosition().lng();
            var markerAction = document.getElementsByName(this.markerId + '.Action')[0];
            if (markerAction.value == null || markerAction.value == "") {
                markerAction.value = "update";
            }
            selectMarker(this);
        });
    }
    return thisMarkerId;
}

function initMap(mapId) {    
    var markerAmount = document.getElementById(mapId + "_marker_amount").value;    
    var markerLocation = new google.maps.LatLng(-34.91799473178151, -56.166508197784424);
    
	var options = { zoom: 1, center: markerLocation, mapTypeId: google.maps.MapTypeId.TERRAIN };
	googleMap = new google.maps.Map(document.getElementById(mapId), options);
    mapBounds = new google.maps.LatLngBounds();
    infoWindow = new google.maps.InfoWindow({ content: "Loading..." });

    for (newMarkerId = 0; newMarkerId < markerAmount; ) {
        var thisMarkerId = '[' + newMarkerId + ']';
	    addMarker(document.getElementsByName(thisMarkerId + '.AreaName')[0].value,
                document.getElementsByName(thisMarkerId + '.AreaInfo')[0].value,
                document.getElementsByName(thisMarkerId + '.Latitude')[0].value,
                document.getElementsByName(thisMarkerId + '.Longitude')[0].value, true);
	}
	if (currentMarker == null) {
	    googleMap.setCenter(markerLocation);
	} else if (markerAmount == 1) {
	    googleMap.setCenter(currentMarker.getPosition());
	} else {
	    googleMap.fitBounds(mapBounds);
	}
}